package com.studorm.xml.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.Node;

import com.opensymphony.xwork2.ActionSupport;
import com.studorm.MD5;
import com.studorm.XMLOption;

/**
 * @author Waiting_hao
 * 
 * 用户登录验证
 *
 */
public class LoginAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3589067291260577880L;

	/*
	 * 登录用户的姓名
	 */
	//String name = null;

	/*
	 * 是否登录成功 
	 */
	//Boolean login = false;

	/*
	 * 用户身份
	 * 1、超级管理员
	 * 2、宿舍管理员
	 * 3、学生
	 */
	int identity = 0;


	public int getIdentity() {
		return identity;
	}


	public void setIdentity(int identity) {
		this.identity = identity;
	}


	/* -----------------------------
	 * 		登录验证   ver.12.02
	 * -----------------------------
	 * 
	 * 从前台获取账号，密码，身份等参数后
	 * 通过不同的身份查询不同的表，判断用
	 * 户是否合法。
	 * 
	 */
	public String checkIdentity() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		String identity_s = request.getParameter("identity_radio");
		String autoL = request.getParameter("auto");

		HttpServletResponse response = ServletActionContext.getResponse();

		if(id == null || id.isEmpty()){
			return ERROR;
		}
		if(pw == null || pw.isEmpty() || pw.length() < 6){
			return ERROR;
		}
		if(identity_s == null | identity_s.isEmpty()){
			return ERROR;
		}
		identity = Integer.parseInt(identity_s);

		int count = 0;
		Document checkDoc = null;
		Node checkNode = null;

		if(identity == 3){
			String studentsinfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\studentsinfs.xml");
			checkDoc = XMLOption.loadXML(studentsinfsPath);
			checkNode = checkDoc.selectSingleNode("/studentsinfs/studentsinf[@studentNu='"+id+"']");
		}
		if(identity == 2){
			String staffinfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\staffinfs.xml");
			checkDoc = XMLOption.loadXML(staffinfsPath);
			checkNode = checkDoc.selectSingleNode("/staffinfs/staffinf[@staffNu='"+id+"']");
		}
		if(checkNode == null){
			identity = 0;
			return NONE;
		}


		/*
		 * 从cookie中读取的密码已加密
		 * 无须再次加密
		 */
		if(autoL != null && autoL.equals("true")){
			if(pw.equals(checkNode.valueOf("password"))){
				count = 1;
			}
		}else{
			if(MD5.getMD5Str(pw).equals(checkNode.valueOf("password"))){
				count = 1;
			}
		}

		if(count == 1){
			setSession(checkNode, request, identity, id);
			String remember = request.getParameter("remb");
			if(remember != null && remember.equals("checked")){
				Cookie cookie_id = new Cookie("id", id);
				Cookie cookie_pw = new Cookie("pw", MD5.getMD5Str(pw));
				Cookie cookie_identity = new Cookie("identity", String.valueOf(identity));
				cookie_id.setMaxAge(2592000); // 30天
				cookie_pw.setMaxAge(2592000);
				cookie_identity.setMaxAge(2592000);

				response.addCookie(cookie_pw);
				response.addCookie(cookie_id);
				response.addCookie(cookie_identity);
			}

			if(autoL != null && autoL.equals("true")){
				switch(identity){
				case 1:
					response.sendRedirect("/admin/admin.jsp");// 有问题
					break;
				case 2:
					response.sendRedirect("/studormcms/staff/staff.jsp");
					break;
				case 3:
					response.sendRedirect("/studormcms/student/student.jsp");
					break;
				}
			}
		}else{
			identity = 0;
		}

		return SUCCESS;
	}


	private void setSession(Node checkNode, HttpServletRequest request, int identity, String id) throws Exception {
		HttpSession session = request.getSession();
		session.setAttribute("isLogin", String.valueOf(identity));
		session.setAttribute("id", id);

		session.setAttribute("name", checkNode.valueOf("name"));

	}

}
